<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/1/3
 * Time: 18:26
 */

namespace app\index\controller;
use app\index\validate\Valid;
use think\Request;
class Tibhbftj extends Common
{
    public function index(){
        $sn = $this->request->get('sn');
        $this->assign('sn',$sn);
        $where = [];
        if($sn){
            $where['sn'] = ['like',"%{$sn}%"];
        }
        $count = db('cg_ti_hexiao')->where($where)->field('id,sn,tax,yzfje')->count();
        $data = db('cg_ti_hexiao')->where($where)->field('id,sn,tax,yzfje')->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $t1 = 0;
        $t2 = 0;
        $t3 = 0;
        $t4 = 0;
        foreach($data as $k=>$v){
            $hbje = db('cg_order_list')->where(['xslx'=>3,'hxdmxbh'=>$v['id']])->sum('ysje');
            $bfje = db('cg_order_list')->where(['xslx'=>4,'hxdmxbh'=>$v['id']])->sum('bfje');
            $v['bhjbfzje'] = $hbje+$bfje;
            $v['wzfje'] = $v['tax']-$v['yzfje']-$v['bhjbfzje'];
            $t1 += $v['tax'];
            $t2 += $v['bhjbfzje'];
            $t3 += $v['yzfje'];
            $t4 += $v['wzfje'];
            $data[$k] = $v;
        }
        $this->assign([
            't1'=>$t1,
            't2'=>$t2,
            't3'=>$t3,
            't4'=>$t4,
            'data'=>$data,
            'page'=>$page,
        ]);
        return $this->fetch();
    }

    /**
     * 销售订单列表
     * @return mixed
     */
    public function xiaoshoudingdan_List(){
        $hx_id = $this->request->param('hx_id');
        if(!$hx_id)
            return '';
        $zoning = $this->request->get('zoning');
        $this->assign('zoning',$zoning);
        $provice = $this->request->get('provice');
        $this->assign('provice',$provice);
        $city = $this->request->get('city');
        $this->assign('city',$city);
        $ordersn = $this->request->get('ordersn');
        $this->assign('ordersn',$ordersn);
        $jxssn = $this->request->get('jxssn');
        $this->assign('jxssn',$jxssn);
        $jxsname = $this->request->get('jxsname');
        $this->assign('jxsname',$jxsname);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $minNum = $this->request->get('minNum');
        $this->assign('minNum',$minNum);
        $maxNum = $this->request->get('maxNum');
        $this->assign('maxNum',$maxNum);
        $applyStatus = $this->request->get('applyStatus');
        $this->assign('applyStatus',$applyStatus);
        $sqrmc = $this->request->get('sqrmc');
        $this->assign('sqrmc',$sqrmc);
        $fhzt = $this->request->get('fhzt');
        $this->assign('fhzt',$fhzt);

        $where = [];
            $where['id'] = ['exp','in(select order_id from cg_order_list where hxdmxbh='.$hx_id.')'];

        $cus_ids = [];
        if($zoning){
            $cus_ids = db('cg_market_zoning')->where('title','like',"%{$zoning}%")->column('id');
        }
        if($provice){
            $provice_ids = db('cg_market_provice')->where('title','like',"%{$provice}%")->column('id');
            $cus_ids = $cus_ids?array_intersect($cus_ids,$provice_ids):$provice_ids;
        }
        if($city){
            $city_ids = db('cg_market_city')->where('title','like',"%{$city}%")->column('id');
            $cus_ids = $cus_ids?array_intersect($cus_ids,$city_ids):$city_ids;
        }
        if($ordersn){
            $where['sn'] = ['like',"%{$ordersn}%"];
        }
        if($jxssn){
            $jxsids = db('cg_jxs')->where('sn','like',"%{$jxssn}%")->column('id');
            $cus_ids = $cus_ids?array_intersect($cus_ids,$jxsids):$jxsids;
        }
        if($jxsname){
            $jxsids = db('cg_jxs')->where('title','like',"%{$jxsname}%")->column('id');
            $cus_ids = $jxsids?array_intersect($cus_ids,$jxsids):$jxsids;
        }
        if($start&&$end){
            $where['jyfhrq'] = [['>',$start],['<',$end],'and'];
        }elseif($start){
            $where['jyfhrq'] = ['>',$start];
        }elseif($end){
            $where['jyfhrq'] = ['<',$end];
        }
        if($minNum&&$maxNum){
            $where['yszje'] = [['>',$minNum],['<',$maxNum],'and'];
        }elseif($minNum){
            $where['yszje'] = ['>',$minNum];
        }elseif($maxNum){
            $where['yszje'] = ['<',$maxNum];
        }
        if($applyStatus){
            $where['status'] = $applyStatus;
        }
        $cus_ids = array_unique($cus_ids);
        if($cus_ids){
            $where['cusName'] = ['in',$cus_ids];
        }

        if($sqrmc){
            $where['sqrmc'] = ['in',$sqrmc];
        }

        if($fhzt!==null&&$fhzt!==''){
            $where['fhzt'] = $fhzt;
        }

        $cnt = db('cg_order')->where($where)->count();
        $data = db('cg_order')->where($where)->paginate(10,$cnt);
        $page = $data->render();
        $data = $data->items();

        foreach($data as $k=>$v){
            $t = $v['cusName'];
            $v['zoning'] = db('cg_market_zoning t')->join('cg_jxs t2','t2.zoning=t.id')->where('t2.id',$t)->value('t.title');
            $v['provice'] = db('cg_market_provice t')->join('cg_jxs t2','t2.provice=t.id')->where('t2.id',$t)->value('t.title');
            $v['city'] = db('cg_market_city t')->join('cg_jxs t2','t2.city=t.id')->where('t2.id',$t)->value('t.title');
            $v['je'] = db('cg_order_list')->where('order_id',$v['id'])->sum('ysje');
            $data[$k] = $v;
        }

        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }


}